#ifndef BOARD_H__
#define BOARD_H__

// ==== General ====
#define BOARD_NAME          "Silmaril2v1"
#define APP_NAME            "Silmarili2"

#define STM32G070xx

#define LSE_FREQ_HZ     32768   // Left it here even if not used

// ST driver system settings
#define STM32_ST_IRQ_PRIORITY   2
#define STM32_ST_USE_TIMER      15

#ifndef TRUE
#define TRUE            1
#endif

// Freq of external crystal if any. Leave it here even if not used.
#define CRYSTAL_FREQ_HZ     12000000UL

#if 1 // ==== Flash ====
#define FLASH_SZ            0x20000UL // 128k
#define FLASH_PAGE_SZ       2048UL // Constant, see ds
#define SETTINGS_SZ         FLASH_PAGE_SZ
#define BL_SZ_MAX           8192UL // Max sz of bootloader
#define APP_SZ_MAX          (FLASH_SZ - BL_SZ_MAX - SETTINGS_SZ)
#define FLASH_SETTINGS_ADDR (FLASH_BASE + FLASH_SZ - SETTINGS_SZ)
#define APP_START_ADDR      (FLASH_BASE + BL_SZ_MAX)
#endif

#if 1 // ========================== GPIO =======================================
// ==== LEDs ====
#define TIMESLOT_TOP    255UL
// Central LED uses PWM timer and therefore requires 255*255 brightness steps.
#define CENTRAL_LED     { GPIOA, 6, AF5, TIM16, 1, invNotInverted, omPushPull, (TIMESLOT_TOP + 1UL) }
#define SYS_LED         { GPIOA, 8, AF2,  TIM1, 1, invNotInverted, omPushPull, 255 }
// LEDs regs control
#define LED_PWR_EN      GPIOA, 5
#define OUT_EN_PIN      GPIOA, 7
#define LATCH_PIN       GPIOB, 1, omPushPull, pudNone, AF1 // T3C4
#define LED_CLK         GPIOB, 3, omPushPull, pudNone, AF0
#define LATCH_TMR_IN    GPIOB, 4, omPushPull, pudPullDown, AF1 // T3C1
#define LED_DATA        GPIOB, 5, omPushPull, pudNone, AF0
#define LATCH_TMR       TIM3
#define LEDS_SPI        SPI1
// Counts
#define LED_CNT_SPI     40UL
#define LED_CNT_TOTAL   (LED_CNT_SPI + 1UL)
#define LED_BYTE_CNT    5UL // 40 bits
#define LED_IC_CNT      5UL

// UART
#define UART_TX_PIN     GPIOA, 9, AF1
#define UART_RX_PIN     GPIOA, 10, AF1

// Acc
#define ACC_IRQ_PIN     GPIOA, 0
#define ACC_PWR         GPIOB, 2
#define ACC_MISO        GPIOB, 6
#define ACC_MOSI        GPIOB, 7
#define ACC_SCK         GPIOB, 8
#define ACC_CS          GPIOB, 9

// Others
#define IS_CHARGING     GPIOA, 4
#define USB_DETECT      GPIOA, 12
// DIP
#define DIP_PIN1        GPIOC, 6
#define DIP_PIN2        GPIOC, 14
#define DIP_PIN3        GPIOC, 15
#define DIP_PIN4        GPIOA, 11

// Debug pin
#define DBG_PIN         GPIOB, 0
#define DBG_HI()        PinSetHi(DBG_PIN)
#define DBG_LO()        PinSetLo(DBG_PIN)
#define DBG_TOGGLE()    PinToggle(DBG_PIN)

#endif // GPIO

#if 1 // =========================== I2C =======================================
#define I2C1_ENABLED        FALSE
#define I2C2_ENABLED        FALSE
#define I2C_BAUDRATE_HZ     400000
#define I2C_USE_DMA         FALSE
#endif

#if 1 // =========================== ADC =======================================
#define ADC_ENABLED     TRUE
#define ADC_BAT_IN      GPIOA, 1
#define ADC_BAT_EN      GPIOA, 2
#define ADC_CHNL_VBAT   1
#define ADC_CHNL_CNT    2 // VBat and VRef
#endif

#if 1 // =========================== Timers ====================================
#define LEDS_TIM        TIM3
#define LEDS_TIM_IRQ_VECTOR TIM3_IRQHandler

#define LEDS_DIM_TIM    TIM14
#define LEDS_DIM_CHNL   1

#define TIME_TIMER      TIM7
#define ADC_TIMER       TIM6

#endif


#if 1 // =========================== DMA =======================================
// ==== Uart ====
#define UART_DMA_TX_CHNL    7
#define UART_DMA_TX_REQID   51 // p223 of refman
#define UART_DMA_RX_CHNL    6
#define UART_DMA_RX_REQID   50 // p223 of refman

// ==== LEDs ====
#define LEDS_DMA_TX_CHNL    1
#define LEDS_DMA_TX_REQID   17 // SPI1 TX, p223 of refman


// ==== I2C1 ====
#define I2C1_DMA_TX_CHNL    5
#define I2C1_DMA_TX_REQID   11
#define I2C1_DMA_RX_CHNL    4
#define I2C1_DMA_RX_REQID   10

// ==== ADC ====
//#define ADC_DMA_CHNL
//#define ADC_DMA_REQID       5

#endif // DMA

#if 1 // ========================== USART ======================================
#define PRINTF_FLOAT_EN     FALSE
#define UART_TXBUF_SZ       512
#define UART_RXBUF_SZ       256
#define CMD_BUF_SZ          128
#define UART_CHAR_TO_IRQ    '\n'

#define CMD_UART_PARAMS \
    USART1, UART_TX_PIN, UART_RX_PIN, \
    UART_DMA_TX_CHNL, UART_DMA_RX_CHNL, \
    UART_DMA_TX_REQID, UART_DMA_RX_REQID, \
    uartclkPCLK

#endif

#endif //BOARD_H__
